package com.leyou.user.web;

import com.leyou.auth.dto.UserDetails;
import com.leyou.auth.utils.UserContext;
import com.leyou.common.exceptions.LyException;
import com.leyou.user.dto.UserDTO;
import com.leyou.user.dto.UserDetailDTO;
import com.leyou.user.entity.User;
import com.leyou.user.entity.UserDetail;
import com.leyou.user.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("auth")
public class UserAuthController {

    private final UserService userService;

    public UserAuthController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 查询当前登录用户
     *
     * @return 登录用户信息
     */
    @GetMapping("/detail")
    public ResponseEntity<UserDetailDTO> detail() {
        UserDetailDTO userDetailDTO = userService.myDetail(UserContext.getUser().getId());
        return ResponseEntity.ok(userDetailDTO);
    }

    @PostMapping("/detail/save")
    public ResponseEntity<UserDetailDTO> saveDetail(@RequestParam(value = "nickname", required = false, defaultValue = "") String nickname,
                                                    @RequestParam(value = "gender", required = false, defaultValue = "") String gender,
                                                    @RequestParam(value = "job", required = false, defaultValue = "") String job,
                                                    @RequestParam(value = "birthday", required = false, defaultValue = "") String birthday,
                                                    @RequestParam(value = "address", required = false, defaultValue = "") String address) {

        UserDetail userDetail = new UserDetail();
        userDetail.setUserId(UserContext.getUser().getId());
        userDetail.setNickname(nickname);
        userDetail.setGender(gender);
        userDetail.setBirthday(birthday);
        userDetail.setAddress(address);
        userDetail.setJob(job);
        userService.myDetailSave(userDetail);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PostMapping("/detail/saveimg")
    public ResponseEntity<UserDetailDTO> saveDetail(@RequestParam("img") String img) {
        UserDetail userDetail = new UserDetail();
        userDetail.setUserId(UserContext.getUser().getId());
        userDetail.setImg(img);
        userService.myDetailSave(userDetail);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
}
